package gsw.demo.greendao;

import android.content.Context;

import org.greenrobot.greendao.database.Database;

/**
 * 创建DaoSession
 */
public class DaoSessionFactory {
    //GreenDao是否需要加密数据库；加密数据库还需要引入SQLCipher框架
    public static final boolean ENCRYPTED = false;
    //GreenDao的数据库连接
    private static DaoSession daoSession = null;

    /**
     * 创建DaoSession
     *
     * @return 返回DaoSession
     */
    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            synchronized (DaoSessionFactory.class) {
                if (daoSession == null) {
                    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
                    Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
                    daoSession = new DaoMaster(db).newSession();
                }
            }
        }
        return daoSession;
    }
}
